package org.leetcode.middle.leetcode3;

import java.util.HashSet;

public class Solution2 {

    public static void main(String[] args) {
        String s = "pwwkew";
        int length = lengthOfLongestSubstring(s);
        System.out.println(length);
    }
    public static int lengthOfLongestSubstring(String s) {
        int maxLength = 0;

        int n = s.length();

        int slow = 0;
        int fast = 0;
        HashSet<Character> hashSet = new HashSet<>();
        while (fast<n){
            if (slow>0){
                hashSet.remove(s.charAt(slow-1));
            }
            while (fast<n && !hashSet.contains(s.charAt(fast))){
                hashSet.add(s.charAt(fast));
                fast++;
            }
            maxLength=Math.max(maxLength,fast-slow);
            slow++;
        }

        return maxLength;
    }
}
